﻿@model List<ETL.Business.Models.ProjectModel>

<!--   Ajax functions   -->
<script type="text/javascript">
    function LoadProjects(openProjectValue, closeProjectValue) {
        $.ajax({
            url: "/EnTimeLog/GetProjectPartial",
            type: 'POST',
            data: { chkOpenProjects: openProjectValue, chkClosedProjects: closeProjectValue },
            success: function (response) {
                $("#ajaxDropDownListProject").replaceWith(response);
                ClearGridData();
            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }

    /*  Get project detailed information    */
    function GetProjectDetail(pr_ref) {
        $.ajax({
            url: "/EnTimeLog/GetProjectDetail",
            type: 'POST',
            data: { projectId: pr_ref },
            success: function (response) {

                // Split response to array of values
                var array = response.split("#");

                // Replace old values with new ones
                $("#inputEngineer").val(array[0]);
                $("#inputLeadEngineer").val(array[1]);
                $("#inputToStart").val(array[2]);
                $("#inputToFinish").val(array[3]);
                $("#inputStatus").val(array[4]);
                $("#inputCompleted").val(array[5]);

            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }

    /*  Get project categories    */
    function GetProjectCategories(pr_ref) {
        $.ajax({
            url: "/EnTimeLog/GetCategory",
            type: 'POST',
            data: { projectId: pr_ref },
            success: function (response) {
                $("#ajaxGridCategory").replaceWith(response);
                $("#ajaxGridTask").replaceWith("<div id='ajaxGridTask'></div>");
            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }

    /*  Get other engineers in the selected projects    */
    function GetListOtherEngineers(pr_ref) {
        $.ajax({
            url: "/EnTimeLog/GetOtherEngineers",
            type: 'POST',
            data: { projectId: pr_ref },
            success: function (response) {
                $("#ajaxListOtherEngineers").replaceWith(response);
            },
            error: function (xhr) {
                alert("Error!");
            }
        });
    }

    /*  Clear all grid data    */
    function ClearGridData() {
        $("#ajaxGridCategory").replaceWith("<div id='ajaxGridCategory'></div>");
        $("#ajaxGridTask").replaceWith("<div id='ajaxGridTask'></div>");
        $("#ajaxListOtherEngineers").replaceWith("<div id='ajaxListOtherEngineers'></div>");
    }

</script>

<!--   Js event functions   -->
<script type="text/javascript">
    $(function () {
        $("select#ddlProject").change(function (evt) {
            if ($("select#ddlProject").val() != "-1" && $("select#ddlProject").val() != "" && $("select#ddlProject").val() != null) {

                // Get project referenced value
                var pr_ref = $("select#ddlProject").val();

                // Get project detailed info and categories, other engineers
                GetProjectDetail(pr_ref);
                GetProjectCategories(pr_ref);
                GetListOtherEngineers(pr_ref);
            }
            else {
                // Clear all grid's data
                ClearGridData();
            }
        });

        $('#chkOpenProjects').click(function () {
            if ($(this).attr("checked") == "checked") {
                // Do stuff
                $("#chkClosedProjects").removeAttr('checked');
                LoadProjects("LoadOpenProjects", "");
            }
            else { LoadProjects("", ""); }
        });


        $('#chkClosedProjects').click(function () {
            if ($(this).attr("checked") == "checked") {
                // Do stuff
                $("#chkOpenProjects").removeAttr('checked');
                LoadProjects("", "LoadClosedProjects");
            }
            else { LoadProjects("", ""); }
        });

    });
</script>
<div id="ajaxDropDownListProject">
    <select id="ddlProject" style="width: 580px;">
        <option value='-1' title='Select a category...'>Select project...</option>
        @if (Model != null && Model.Count() > 0)
        {
            foreach (var project in Model)
            {
            <option selected="@project.Selected" value='@project.ProjectId'>
                @project.DisplayedName
            </option>
            }
        }
    </select>
</div>